﻿Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Linq
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Data.SqlServerCe
Imports System.Text

Namespace EfiBase

    Public Class GpsCoordenadas

        '-----------------------------------------------------------------------------------

        'Coordenadas Padrões do Escritório da Sulgipe na Fáb. Santa Cruz - © Google. 

        'S11º 16.952 -> -11.16952 -> (GG.MMSSsss) [-11.282533]
        'W37º 26.730 -> -37.26730 -> (GG.MMSSsss) [-37.445500]

        'Zona: 24
        'Hemisfério: S

        '-11.282533 (S) -> UTM X (easting):   669685.774
        '-37.445500 (W) -> UTM Y (northing): 8752330.001

        '-----------------------------------------------------------------------------------
        Public DefaultUtmX = 669681.896  'x = easting
        Public DefaultUtmY = 8752315.788 'y = northing

        Public DefaultLatitude = -11.282533 'Latitude
        Public DefaultLongitude = -37.4455 'Longitude

        Public DefaultLat As String = "S11º 16.952" 'Latitude
        Public DefaultLong As String = "W37º 26.730" 'Longitude

        '-----------------------------------------------------------------------------------

        Private myDecimalLatitude = DefaultLatitude
        Private myDecimalLongitude = DefaultLongitude


        Public Property myLatitude() As Object
            Get
                Return myDecimalLatitude
            End Get
            Set(ByVal value)
                myDecimalLatitude = value
            End Set
        End Property

        Public Property myLongitude() As Object
            Get
                Return myDecimalLongitude
            End Get
            Set(ByVal value)
                myDecimalLongitude = value
            End Set
        End Property

        Public ls As Lista = New Lista()
        Public lst As List(Of Lista) = New List(Of Lista)()

        Public Function cLLToUTM(ByVal decLatitude As Decimal, ByVal decLongitude As Decimal) As UTM
            Dim txtNorthing As Double = 0
            Dim txtEasting As Double = 0
            Dim txtzone As String
            Dim cmbhemisphere As String = UTMLat.South

            Dim lat As New Coordinate(decLatitude, CoordinateType.Latitude)
            Dim lng As New Coordinate(decLongitude, CoordinateType.Longitude)
            Dim cUtm As New UTM(lat, lng)

            txtNorthing = cUtm.Northing
            txtEasting = cUtm.Easting
            txtzone = cUtm.Zone.ToString
            cmbhemisphere = If(cUtm.UTMLatitudeHemisphere = UTMLat.North, "N", "S")

            Return cUtm

        End Function

        Public Sub LLToUTM(ByVal decLatitude As Double, ByVal decLongitude As Double)
            Dim txtNorthing As String
            Dim txtEasting As String
            Dim txtzone As String
            Dim cmbhemisphere As String

            Dim lat As New Coordinate(decLatitude, CoordinateType.Latitude)
            Dim lng As New Coordinate(decLongitude, CoordinateType.Longitude)
            Dim cUtm As New UTM(lat, lng)


            txtNorthing = cUtm.Northing.ToString '<<-This is 3485991.16655783
            txtEasting = cUtm.Easting.ToString '<<-This is 357546.073706798
            txtzone = cUtm.Zone.ToString ' <<-This is 46
            cmbhemisphere = If(cUtm.UTMLatitudeHemisphere = UTMLat.North, "N", "S") 'N
            TUMToLL(cUtm.Northing, cUtm.Easting, cUtm.UTMLatitudeHemisphere)

        End Sub


        Public Sub TUMToLL(ByVal MyNorthing As Double, ByVal myEasting As Double, ByVal myzone As String)
            Dim txtLatitude As String
            Dim txtLongitude As String
            Dim cUtm As New UTM(MyNorthing, myEasting, myzone)

            cUtm.UTMLatitudeHemisphere = UTMLat.North
            txtLatitude = cUtm.Latitude.ToString '<<-This is 31° 29' 59.989" North
            txtLongitude = cUtm.Longitude.ToString '<<-This is '0° 0' 0.000" North

        End Sub

        'Enums
        Public Enum CardinalDirection
            North
            East
            South
            West
        End Enum

        Public Enum UTMLat
            North = CardinalDirection.North
            South = CardinalDirection.South
        End Enum

        Public Enum CoordinateType
            Latitude
            Longitude
        End Enum

        Public Enum UTMDatum
            SIRGAS2000
            WGS84
            NAD83
            GRS80
            WGS72
            SAD69
            Australian1965
            Krasovsky1940
            NorthAmerican1927
            International1924
            Hayford1909
            Clarke1880
            Clarke1866
            Airy1830
            Bessel1841
            Everest1830
        End Enum

        'Structures
        Public Structure Coordinate
            Public Degrees As Int32
            Public Minutes As Int32
            Public Seconds As Decimal
            Public Direction As CardinalDirection

            Public Sub New(ByVal Degrees As Int32, ByVal Minutes As Int32, ByVal seconds As Decimal, ByVal Direction As CardinalDirection)
                Me.Degrees = Degrees
                Me.Minutes = Minutes
                Me.Seconds = seconds
                Me.Direction = Direction
            End Sub

            Public Function Truncate(ByVal d As Decimal, ByVal fator As Byte) As Decimal

                Return Int(d * (10 ^ fator)) / (10 ^ fator)

            End Function

            Public Sub New(ByVal Degrees As Decimal, ByVal Type As CoordinateType)
                If Type = CoordinateType.Latitude Then
                    If Degrees > 90 OrElse Degrees < -90 Then Exit Sub
                    Me.Direction = If(Degrees < 0, CardinalDirection.South, CardinalDirection.North)
                Else
                    If Degrees > 180 OrElse Degrees < -180 Then Exit Sub
                    Me.Direction = If(Degrees < 0, CardinalDirection.West, CardinalDirection.East)
                End If

                Degrees = Math.Abs(Degrees)
                Me.Degrees = Convert.ToInt32(Truncate(Degrees, 0))
                Me.Minutes = Convert.ToInt32(Truncate((Degrees - Truncate(Degrees, 0)) * 60D, 0))
                Me.Seconds = (Degrees - Truncate(Degrees, 0) - Convert.ToDecimal(Me.Minutes) / 60) * 3600
            End Sub

            Public Overrides Function ToString() As String
                Return String.Format("{0}° {1}' {2:f3}"" {3}", Degrees, Minutes, Seconds, Direction)
            End Function

            Private Function FlipDirection(ByVal Direction As CardinalDirection) As CardinalDirection
                Select Case Direction
                    Case CardinalDirection.North
                        Return CardinalDirection.South
                    Case CardinalDirection.East
                        Return CardinalDirection.West
                    Case CardinalDirection.South
                        Return CardinalDirection.North
                    Case CardinalDirection.West
                        Return CardinalDirection.East
                End Select
            End Function

            Public Function GetAbsoluteDecimalCoordinate() As Decimal
                Return Convert.ToDecimal(Degrees) + Convert.ToDecimal(Minutes) / 60D + Seconds / 3600D
            End Function

            Public Function GetDecimalCoordinate() As Decimal
                Dim dec As Decimal = Convert.ToDecimal(Degrees) + Convert.ToDecimal(Minutes) / 60D + Seconds / 3600D
                Return If((Direction = CardinalDirection.North OrElse Direction = CardinalDirection.East), dec, -dec)
            End Function
        End Structure

    End Class

End Namespace